package com.cms.demo.controller;

import com.cms.demo.entity.pojo.Book;
import com.cms.demo.entity.pojo.GsonBook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wangning
 * @create 2021-04-25 16:25
 */
@Controller
public class BookController {
	@GetMapping("/books")
	public ModelAndView books() {
		List<Book> books = new ArrayList<>();
		Book b1 = new Book();
		b1.setId(1);
		b1.setName("三国演义");
		b1.setAuthor("罗贯中");
		Book b2 = new Book();
		b2.setId(2);
		b2.setName("西游记");
		b2.setAuthor("吴承恩");
		books.add(b1);
		books.add(b2);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("books", books);
		modelAndView.setViewName("books");
		return modelAndView;
	}

	@GetMapping("books01")
	public ModelAndView books01() {
		List<Book> books = new ArrayList<>();
		Book b1 = new Book();
		b1.setId(1);
		b1.setName("三国演义");
		b1.setAuthor("罗贯中");
		Book b2 = new Book();
		b2.setId(2);
		b2.setName("西游记");
		b2.setAuthor("吴承恩");
		books.add(b1);
		books.add(b2);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("books", books);
		modelAndView.setViewName("books01");
		return modelAndView;
	}

//	@GetMapping("/book")
//	@ResponseBody
//	public Book book() {
//		Book book = new Book();
//		book.setId(1);
//		book.setAuthor("罗贯中");
//		book.setName("三国演义");
//		book.setPrice(25.5f);
//		book.setDate(new Date());
//		return book;
//	}
	@GetMapping("/gsonbook")
	@ResponseBody
	public GsonBook gsonbook() {
		GsonBook book = new GsonBook();
		book.setId(1);
		book.setAuthor("罗贯中");
		book.setName("三国演义");
		book.setPrice(25.5f);
		book.setDate(new Date());
		return book;
	}

	/**
	 * 跨域有两种配置，一个可以直接写在响应的请求方法上加注解
	 * @return
	 */
	@GetMapping("/add")
	@ResponseBody
	@CrossOrigin(value = "http://localhost:8090",maxAge = 1800,allowedHeaders = "*")
	public String addBook() {

		return "添加成功";
	}
}
